package com.jh3502.vivo.function.author.chosen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.author.AuthorListBean;
import com.jh3502.vivo.function.author.AuthorPresenter;
import com.jh3502.vivo.function.author.add.AuthorBean;
import com.jh3502.vivo.view.MyRecyclerView;
import com.jh3502.vivo.view.treeview.BranchNode;
import com.jh3502.vivo.view.treeview.LayoutItem;
import com.jh3502.vivo.view.treeview.RootNode;
import com.jh3502.vivo.view.treeview.TreeNode;
import com.jh3502.vivo.view.treeview.TreeViewAdapter;
import com.jh3502.vivo.view.treeview.TreeViewBinder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author:harisucici on 2018/11/7 10:19
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class AuthorChosenActivity extends BaseActivity<AuthorPresenter.AuthorView, AuthorPresenter> implements AuthorPresenter.AuthorView {

    @BindView(R.id.author_recycle)
    MyRecyclerView authorRecycle;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private List<TreeNode> dataAll = new ArrayList<>();
    private List<AuthorBean> dataChosen = new ArrayList<>();
    private TreeViewAdapter adapter;
    private boolean hidebutton = false;
    private boolean singlechosen = false;

    @Override
    public AuthorPresenter initPresenter() {
        return new AuthorPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_author_chosen;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("授权事件");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mPresenter.setAuthorView(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
        }
        return true;
    }

    @Override
    protected void initData() {
        mPresenter.toFindPermissList();


    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }


    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);

    }

    @Override
    public void requestFailure(Throwable t) {

    }


    private void initAdapter(boolean b) {
        adapter = new TreeViewAdapter(dataAll,
                Arrays.asList(new RootViewBinder(), new BranchViewBinder(b))) {
            @Override
            public void toggleClick(TreeViewBinder.ViewHolder viewHolder, View view, boolean isOpen, TreeNode treeNode) {
                adapter.lastToggleClickToggle();
            }

            @Override
            public void toggled(TreeViewBinder.ViewHolder viewHolder, View view, boolean isOpen, TreeNode treeNode) {
                viewHolder.findViewById(R.id.root_node).setRotation(isOpen ? 180 : 0);
            }

            @Override
            public void checked(TreeViewBinder.ViewHolder viewHolder, View view, boolean checked, TreeNode treeNode) {
                AuthorBean bean = (AuthorBean) ((BranchNode) treeNode.getValue()).getBean();
                if (singlechosen) {
                    dataChosen.clear();
                    dataChosen.add(bean);
                } else {
                    if (checked) {
                        dataChosen.add(bean);
                    } else {
                        dataChosen.remove(bean);
                    }
                }
            }

            @Override
            public void itemClick(TreeViewBinder.ViewHolder viewHolder, View view, TreeNode treeNode) {
                LayoutItem item = treeNode.getValue();
                if (item instanceof RootNode) {
                    viewHolder.findViewById(R.id.root_node).performClick();
                } else if (item instanceof BranchNode) {
                    AuthorBean authorBean = (AuthorBean) ((BranchNode) item).getBean();
//                    Bundle bundle = new Bundle();
//                    bundle.putString("dep", authorBean.getDeptName());
//                    bundle.putString("name", authorBean.getName());
//                    bundle.putString("postname", authorBean.getPostName());
//                    bundle.putString("telephone", authorBean.getTelephone());
//                    bundle.putString("mobile", authorBean.getMobile());
//                    Utils.getInstance().startActivity(mActivity, ContactDetailActivity.class, bundle);
                }

            }
        };
        adapter.setingleChosen(singlechosen);
        authorRecycle.setLayoutManager(new LinearLayoutManager(this));
        authorRecycle.setAdapter(adapter);
    }


    @Override
    public void findPermissListSuccess(String s) {

        List<AuthorBean> list = getAuthor(s);
        TreeSet<Integer> pidSet = new TreeSet<>();
        for (AuthorBean authorBean : list) {
            pidSet.add(authorBean.getType());
        }

        List<List<AuthorBean>> contactList = new ArrayList<>(2);
        for (int m : pidSet) {
            List<AuthorBean> persons = new ArrayList<>(2);
            for (AuthorBean authorBean : list) {
                if (authorBean.getType() == m) {
                    persons.add(authorBean);
                }
            }
            contactList.add(persons);
        }

        for (List<AuthorBean> authorBeans : contactList) {
            TreeNode<RootNode> rootNode =
                    new TreeNode<>(new RootNode(authorBeans.get(0)));
            for (AuthorBean authorBean : authorBeans) {
                TreeNode<BranchNode> branchNode = new TreeNode<>(new BranchNode(authorBean));
                rootNode.addChild(branchNode);
            }
            dataAll.add(rootNode);
            initAdapter(hidebutton);
        }
    }


    @Override
    public void submitEntrustSuccess() {

    }

    @Override
    public void findMyEntrustSuccess(List<AuthorListBean> dataList) {

    }

    @Override
    public void delEntrustSuccess() {

    }

    @Override
    public void enableEntrustSuccess() {


    }

    public ArrayList<AuthorBean> getAuthor(String s) {
        return new Gson().fromJson(s,
                new TypeToken<List<AuthorBean>>() {
                }.getType());
    }

    @OnClick(R.id.author_cancel)
    public void onAuthorCancelClicked() {
        onBackPressed();
    }

    @OnClick(R.id.author_submit)
    public void onAuthorSubmitClicked() {
        Intent intent = new Intent();
        intent.putExtra("datachosen", new Gson().toJson(dataChosen));
        setResult(Activity.RESULT_OK, intent);
        onBackPressed();
    }

}
